home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / SciAn / src / util / smoothrots.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  3KB  |  119 lines

  1. /*smoothrots.c
  2.   Eric Pepke
  3.  
  4.   Emits a script segment containing a series of rotations about an axis 
  5.   and snaps to standard output, with acceleration at the beginning and
  6.   deceleration at the end
  7.  
  8.   Usage:
  9.     rots axis step nSteps
  10.  
  11.   Example:
  12.     rots z 90.0 115 10 > rot115
  13.  
  14.   rotates about the z axis a total of 90 degrees in 115 steps with start
  15.   and stop accelerations of 10 steps each, placing the result in file rot115
  16. */
  17.  
  18. #include <stdio.h>
  19.  
  20. char axis;
  21. double total;
  22.  
  23. void DoFunction(amount, lastAmount)
  24. double amount, lastAmount;
  25. /*Does the function to get to amount (in [0,1]) from lastAmount*/
  26. {
  27.     {
  28.     printf("rotate %c %lg\n", axis, (amount - lastAmount) * total);
  29.     printf("snap\n");
  30.     }
  31. }
  32.  
  33. main(argc, argv)
  34. int argc;
  35. char *argv[];
  36. {
  37.     double minorStep, amount, lastAmount, check;
  38.     long nSteps, nAccel, accelTotal, nInertial, inertialTotal, totalMinorSteps;
  39.     long progress;
  40.     long k;
  41.     if (argc != 5)
  42.     {
  43.         fprintf(stderr, "usage: %s axis total nSteps nAccel\n", argv[0]);
  44.     exit(-1);
  45.     }
  46.     if (1 != sscanf(argv[1], "%c", &axis))
  47.     {
  48.         fprintf(stderr, "usage: %s axis total nSteps nAccel\n", argv[0]);
  49.     exit(-1);
  50.     }
  51.     if (1 != sscanf(argv[2], "%lg", &total))
  52.     {
  53.         fprintf(stderr, "usage: %s axis total nSteps nAccel\n", argv[0]);
  54.     exit(-1);
  55.     }
  56.     if (1 != sscanf(argv[3], "%d", &nSteps))
  57.     {
  58.         fprintf(stderr, "usage: %s axis total nSteps nAccel\n", argv[0]);
  59.     exit(-1);
  60.     }
  61.     if (1 != sscanf(argv[4], "%d", &nAccel))
  62.     {
  63.         fprintf(stderr, "usage: %s axis total nSteps nAccel\n", argv[0]);
  64.     exit(-1);
  65.     }
  66.  
  67.     /*Calculate the number of time steps in the inertial phase*/
  68.     nInertial = nSteps - 2 * nAccel;
  69.     if (nInertial < 0)
  70.     {
  71.     fprintf(stderr, "The total number of steps is not be enough for acceleration.\n");
  72.     exit(-1);
  73.     }
  74.  
  75.     /*Calculate the number of minor steps in each acceleration phase*/
  76.     accelTotal = 0;
  77.     for (k = 1; k <= nAccel; ++k)
  78.     {
  79.     accelTotal += k;
  80.     }
  81.  
  82.     /*Calculate the number of minor steps in the inertial phase*/
  83.     inertialTotal = nInertial * (nAccel + 1);
  84.  
  85.     /*Calculate the total number of minor steps*/
  86.     totalMinorSteps = inertialTotal + 2 * accelTotal;
  87.  
  88.     /*Start off with none done*/
  89.     progress = 0;
  90.     lastAmount = 0;
  91.  
  92.     /*Do acceleration phase*/
  93.     for (k = 1; k <= nAccel; ++k)
  94.     {
  95.     progress += k;
  96.     lastAmount = amount;
  97.     amount = ((double) progress) / ((double) totalMinorSteps);
  98.     DoFunction(amount, lastAmount);
  99.     }
  100.  
  101.     /*Do inertial phase*/
  102.     for (k = 0; k < nInertial; ++k)
  103.     {
  104.     progress += nAccel + 1; 
  105.     lastAmount = amount;
  106.     amount = ((double) progress) / ((double) totalMinorSteps);
  107.     DoFunction(amount, lastAmount);
  108.     }
  109.  
  110.     /*Do deceleration phase*/
  111.     for (k = nAccel; k >= 1; --k)
  112.     {
  113.     progress += k;
  114.     lastAmount = amount;
  115.     amount = ((double) progress) / ((double) totalMinorSteps);
  116.     DoFunction(amount, lastAmount);
  117.     }
  118. }
  119.